package com.qinxueit.yueshu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qinxueit.yueshu.entity.Article;
import com.qinxueit.yueshu.entity.Chapter;
import com.qinxueit.yueshu.service.ChapterService;
import com.qinxueit.yueshu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangqinxue
 * @since 2021-07-26
 */
@RestController
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    /**
     * 所有文章章节
     * @param currentPage
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage){
        Page page = new Page(currentPage,20);
        IPage iPage = chapterService.page(page, new QueryWrapper<Chapter>().orderByDesc("create_time"));
        return Result.success(iPage);
    }

    /**
     * 单张文章内容
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable(name = "id") Long id){
        Chapter chapter = chapterService.getById(id);
        return Result.success(chapter);
    }

    /**
     * 所有文章章节，不含内容
     * @param currentPage
     * @return
     */
    @GetMapping("/chapters")
    public Result chapters(@RequestParam(defaultValue = "1") Integer currentPage){
        Page page = new Page(currentPage,20);
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.select("id","chapter_name");
        IPage iPage = chapterService.page(page, wrapper.orderByDesc("create_time"));
        return Result.success(iPage);
    }
}
